home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / memory.c < prev    next >
C/C++ Source or Header  |  1997-12-07  |  11KB  |  460 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/memory.c 1.9 1997/08/25 09:24:14 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.4a.
  5.  
  6.   Memory management functions
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96,97 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. /*+ The amount of debugging, non-zero for totals, 2 for logging, 4 for printing each call. +*/
  17. #define DEBUG 0
  18.  
  19. /*+ Under Linux, FreeBSD, NetBSD, SGI, Ultrix, AIX and AT&T stdarg is used +*/
  20. #if defined(__linux__) || \
  21.     defined(__FreeBSD__) || defined(__NetBSD__) || \
  22.     defined(__sgi__) || defined(__ultrix__) || \
  23.     defined(_AIX) || defined(_ATT4) || defined(AMIGA)    /* olsen: added `AMIGA' */
  24. #define USE_STD_ARG
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30.  
  31. #ifdef USE_STD_ARG
  32. #include <stdarg.h>
  33. #else
  34. #include <varargs.h>
  35. #endif
  36.  
  37. #ifndef __SASC    /* olsen: does not exist with SAS/C */
  38. #include <memory.h>
  39. #endif /* __SASC */
  40. #include "memory.h"
  41.  
  42. /*+ A private memory heap is used to reduce the number of malloc calls that are made, the Heap type is a pointer to this. +*/
  43. typedef struct _Heap *Heap;
  44.  
  45. /*+ A structure containing all of the information about the private heap in a linked list. +*/
  46. struct _Heap
  47. {
  48.  char* mem;                             /*+ The memory that is private to the heap. +*/
  49.  Heap next;                             /*+ The next Heap structure. +*/
  50. };
  51.  
  52. /*+ Local variable to control the usage of the private heap; +*/
  53. static Heap first=NULL;                 /*+ the first segment of memory on the private heap. +*/
  54. static int heap_left=0;                 /*+ the amount of space left in the current heap segment. +*/
  55.  
  56. static char* get_space(unsigned int l);
  57. static Heap add_to_heap(unsigned int l);
  58.  
  59. #if DEBUG&2
  60. /*+ Variable used for debugging, not a good thing to do. what if more than 16384 mallocs? +*/
  61. static void* addresses[16384];
  62. static char* files[16384];
  63. static int   lines[16384];
  64. #endif
  65. #if DEBUG
  66. /*+ Variable used for debugging. +*/
  67. static int malloc_count=0;
  68. static int realloc_count=0;
  69. static int free_count=0;
  70. #endif
  71.  
  72.  
  73. /*++++++++++++++++++++++++++++++++++++++
  74.   A replacement malloc() function.
  75.  
  76.   void* SafeMalloc Returns the address.
  77.  
  78.   unsigned int size The size of the memory to allocate.
  79.  
  80.   char* file The file that the function is called from.
  81.  
  82.   int line The line number that the function is called from.
  83.   ++++++++++++++++++++++++++++++++++++++*/
  84.  
  85. void* SafeMalloc(unsigned int size,char* file,int line)
  86. {
  87.  void* rptr=malloc(size);
  88.  
  89. #if DEBUG&4
  90.  printf("$$Malloc #%5d of %4d bytes at %08lx (%s:%3d)\n",malloc_count+1,size,(long)rptr,file,line);
  91. #endif
  92. #if DEBUG&2
  93.  if(malloc_count==(sizeof(addresses)/sizeof(addresses[0])))
  94.    {fprintf(stderr,"$$Too many Mallocs to log, edit memory.c\n");exit(3);}
  95.  addresses[malloc_count]=(void*)rptr;
  96.  files[malloc_count]=file;
  97.  lines[malloc_count]=line;
  98. #endif
  99. #if DEBUG
  100.  malloc_count++;
  101.  if(!rptr) printf("$$Warning Malloc() returning NULL (%s:%3d)\n",file,line);
  102. #endif
  103. #if !DEBUG
  104.  if(!rptr) printf("Warning Malloc() returning NULL (%s:%3d)\n",file,line);
  105. #endif
  106.  
  107.  return(rptr);
  108. }
  109.  
  110.  
  111. /*++++++++++++++++++++++++++++++++++++++
  112.   A replacement calloc() function.
  113.  
  114.   void* SafeCalloc Returns the address.
  115.  
  116.   unsigned int n The number of items to allocate.
  117.  
  118.   unsigned int size The size of the memory to allocate.
  119.  
  120.   char* file The file that the function is called from.
  121.  
  122.   int line The line number that the function is called from.
  123.   ++++++++++++++++++++++++++++++++++++++*/
  124.  
  125. void* SafeCalloc(unsigned int n,unsigned int size,char* file,int line)
  126. {
  127.  void* rptr=calloc(n,size);
  128.  
  129. #if DEBUG&4
  130.  printf("$$Calloc #%5d of %4d bytes at %08lx (%s:%3d)\n",malloc_count+1,size,(long)rptr,file,line);
  131. #endif
  132. #if DEBUG&2
  133.  if(malloc_count==(sizeof(addresses)/sizeof(addresses[0])))
  134.    {fprintf(stderr,"$$Too many Mallocs to log, edit memory.c\n");exit(3);}
  135.  addresses[malloc_count]=(void*)rptr;
  136.  files[malloc_count]=file;
  137.  lines[malloc_count]=line;
  138. #endif
  139. #if DEBUG
  140.  malloc_count++;
  141.  if(!rptr) printf("$$Warning Calloc() returning NULL (%s:%3d)\n",file,line);
  142. #endif
  143. #if !DEBUG
  144.  if(!rptr) printf("Warning Calloc() returning NULL (%s:%3d)\n",file,line);
  145. #endif
  146.  
  147.  return(rptr);
  148. }
  149.  
  150.  
  151. /*++++++++++++++++++++++++++++++++++++++
  152.   A replacement realloc() function.
  153.  
  154.   void* SafeRealloc Returns the address.
  155.  
  156.   void* ptr The old pointer.
  157.  
  158.   unsigned int size The size of the new memory to allocate.
  159.  
  160.   char* file The file that the function is called from.
  161.  
  162.   int line The line number that the function is called from.
  163.   ++++++++++++++++++++++++++++++++++++++*/
  164.  
  165. void* SafeRealloc(void* ptr,unsigned int size,char* file,int line)
  166. {
  167.  void* rptr=realloc(ptr,size);
  168.  
  169. #if DEBUG&4
  170.  printf("$$Realloc #%4d of %4d bytes at %08lx (old %08lx) (%s:%3d)\n",realloc_count+1,size,(long)rptr,(long)ptr,file,line);
  171. #endif
  172. #if DEBUG&2
  173.  {
  174.   int i;
  175.   for(i=0;i<malloc_count;i++)
  176.      if(addresses[i]==(void*)ptr)
  177.        {addresses[i]=rptr;break;}
  178.   if(i==malloc_count)
  179.      printf("$$Realloc() called for a non Malloced pointer %08lx (%s:%3d)\n",(long)ptr,file,line);
  180.  }
  181. #endif
  182. #if DEBUG
  183.  realloc_count++;
  184.  if(!rptr) printf("$$Warning Realloc() returning NULL (%s:%3d)\n",file,line);
  185. #endif
  186. #if !DEBUG
  187.  if(!rptr) printf("Warning Realloc() returning NULL (%s:%3d)\n",file,line);
  188. #endif
  189.  
  190.  return(rptr);
  191. }
  192.  
  193.  
  194. /*++++++++++++++++++++++++++++++++++++++
  195.   A replacement free() function.
  196.  
  197.   void* ptr The pointer that is to be freed up.
  198.  
  199.   char* file The file that the function is called from.
  200.  
  201.   int line The line number that the function is called from.
  202.   ++++++++++++++++++++++++++++++++++++++*/
  203.  
  204. void SafeFree(void* ptr,char* file,int line)
  205. {
  206. #if DEBUG&4
  207.  printf("$$Free #%5d at %08lx (%s:%3d)\n",free_count+1,(long)ptr,file,line);
  208. #endif
  209. #if DEBUG&2
  210.  {
  211.   int i;
  212.   for(i=0;i<malloc_count;i++)
  213.      if(addresses[i]==(void*)ptr)
  214.        {addresses[i]=(void*)1;break;} 
  215.   if(i==malloc_count)
  216.      printf("$$Free() called for a non Malloced pointer %08lx (%s:%3d)\n",(long)ptr,file,line);
  217.  }
  218. #endif
  219. #if DEBUG
  220.  free_count++;
  221.  if(!ptr) printf("$$Calling Free() on NULL (%s:%3d)\n",file,line);
  222.  else
  223. #endif
  224. #if !DEBUG
  225.  if(!ptr) printf("Calling Free() on NULL (%s:%3d)\n",file,line);
  226.  else
  227. #endif
  228.  
  229.  free(ptr);
  230. }
  231.  
  232.  
  233. /*++++++++++++++++++++++++++++++++++++++
  234.   A function to copy a string on the public global heap.
  235.  
  236.   char* SafeMallocString Returns the copy of the string.
  237.  
  238.   char* x The string to be copied.
  239.  
  240.   char* file The file that the function is called from.
  241.  
  242.   int line The line number that the function is called from.
  243.   ++++++++++++++++++++++++++++++++++++++*/
  244.  
  245. char* SafeMallocString(char* x,char* file,int line)
  246. {
  247.  char* t=NULL;
  248.  
  249.  if(x)
  250.    {
  251.     t=(char*)SafeMalloc(strlen(x)+1,file,line);
  252.     strcpy(t,x);
  253.    }
  254.  
  255.  return(t);
  256. }
  257.  
  258.  
  259. /*++++++++++++++++++++++++++++++++++++++
  260.   A function to copy a string on the local private memory heap.
  261.  
  262.   char* CopyString Returns the copy of the string.
  263.  
  264.   char* x The string to be copied.
  265.   ++++++++++++++++++++++++++++++++++++++*/
  266.  
  267. char* CopyString(char* x)
  268. {
  269.  char* t=NULL;
  270.  
  271.  if(x)
  272.    {
  273.     t=get_space(strlen(x)+1);
  274.     strcpy(t,x);
  275.    }
  276.  
  277.  return(t);
  278. }
  279.  
  280.  
  281. /*++++++++++++++++++++++++++++++++++++++
  282.   A function to concatenate a number of strings.
  283.  
  284.   char* ConcatStrings Returns the a pointer to the new string.
  285.  
  286.   int n The number of strings
  287.  
  288.   char* s The first string.
  289.  
  290.   ... The other strings, 'n' including 's'.
  291.  
  292.   Any of the strings that are inputs can be NULL, in this case they are quietly ignored.
  293.   ++++++++++++++++++++++++++++++++++++++*/
  294.  
  295. char* ConcatStrings(int n,char* s, ...)
  296. {
  297.  char* t=NULL,*str;
  298.  unsigned int l=0;
  299.  int i;
  300.  va_list ap;
  301.  
  302. #ifdef USE_STD_ARG
  303.  va_start(ap,s);
  304. #else
  305.  va_start(ap);
  306. #endif
  307.  
  308.  for(i=0;i<n;i++)
  309.    {
  310.     if(i)
  311.        str=va_arg(ap, char *);
  312.     else
  313.        str=s;
  314.  
  315.     if(str)
  316.        l+=strlen(str);
  317.    }
  318.  
  319.  va_end(ap);
  320.  
  321.  if(l)
  322.    {
  323.     t=get_space(l+1); t[0]=0;
  324.  
  325. #ifdef USE_STD_ARG
  326.     va_start(ap,s);
  327. #else
  328.     va_start(ap);
  329. #endif
  330.  
  331.     for(i=0;i<n;i++)
  332.       {
  333.        if(i)
  334.